<?php
/**
 * Customers Classes Controller
 *
 * PHP 5.2+
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author
 * @property Auth $Auth
 * @version 0.0.1
 **/
/**
* @property Response $Response
* @property Customer $Customer
* @property CustomerShopperGroup $CustomerShopperGroup */

class ResponsesController extends AppController 
{

    public $sN = 'Response'; 
    public $pN = 'response';
    public $lN = 'responses';    
    public $conditions = array();
    
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel("Customer");
        $this->set('sN', $this->sN);        
        $this->set('pN', $this->pN);
        $this->set('lN', $this->lN);
    }
    public function admin_index($survey_id=null) {     
        $this->loadModel("Survey");
        $this->loadModel("ResponsesQuestionsOption");
        $this->loadModel("Question");
        $this->loadModel("QuestionOption");
        $this->loadModel("SurveyQuestion");
        $this->loadModel("Answer");
        $questions = $this->SurveyQuestion->get_question_by_survey($survey_id);
        
        $responses = $this->Response->find('all',array('conditions'=>array('survey_id'=>$survey_id)));
        
        
        $questions_count = count($questions);
        $responses_count = count($responses);
        
        $survey = $this->Survey->read(null,$survey_id);
        $this->set("survey",  $survey);
        $return = array();
        if($survey['Survey']['type']==Survey::EXAMINATION){
            for($i=0;$i<=$questions_count;$i++){
                for($j=0;$j<=$responses_count;$j++){
                    $return[$i][$j]="";
                    if($j>=1){
                        $return[0][$j] = "{$responses[$j-1]['Customer']['f_name']} {$responses[$j-1]['Customer']['l_name']}";
                    }
                    if($i>=1){
                        $return[$i][0]=$questions[$i-1]['Question']['title'];
                    }
                    if($i>=1 && $j >=1){
                        $option = $this->ResponsesQuestionsOption->get_option($responses[$j-1]["Response"]['id'],$questions[$i-1]['Question']['id']);
                        $answer = $this->Answer->get_answers_by_questions($questions[$i-1]['Question']['id']);
                        $ret = array();
                        foreach ($option as $value) {
                            $check = 0;
                            if(in_array($value['Option']['id'], $answer)){
                                $check=1;
                            }
                            $ret[]=array('check'=>$check,'title'=>$value['Option']['title']);
                        }
                        $return[$i][$j]= $ret;
                    }
                }
            }
        }else{
            for($i=0;$i<=$questions_count;$i++){
                for($j=0;$j<=$responses_count;$j++){
                    $return[$i][$j]="";
                    if($j>=1){
                        $return[0][$j] = "{$responses[$j-1]['Customer']['f_name']} {$responses[$j-1]['Customer']['l_name']}";
                    }
                    if($i>=1){
                        $return[$i][0]=$questions[$i-1]['Question']['title'];
                    }
                    if($i>=1 && $j >=1){
                        $opt=$this->ResponsesQuestionsOption->get_option($responses[$j-1]["Response"]['id'],$questions[$i-1]['Question']['id']);
                        $ret = array();
                        foreach ($opt as $value) {
                            if($value['ResponsesQuestionsOption']['option_id']==0){
                                $ret[]=$value['ResponsesQuestionsOption']['extend'];
                            }else{
                                $ret[]=$value['Option']['title'];
                            }
                        }
                        $return[$i][$j]=$ret;
                    }
                }
            }
        } 
        $this->set("return",$return);
    }
    public function admin_summary($survey_id=null) {
        $this->loadModel("Survey");
        $this->loadModel("ResponsesQuestionsOption");
        $this->loadModel("Question");
        $this->loadModel("QuestionOption");
        $this->loadModel("SurveyQuestion");
        $this->loadModel("Answer");
        $questions = $this->SurveyQuestion->get_question_by_survey($survey_id);
        $responses = $this->Response->find('count',array('conditions'=>array("survey_id"=>$survey_id)));
        $this->set('number_response', $responses);
        $this->set('questions', $questions);
        $this->set("survey",  $this->Survey->read(null,$survey_id));
    }
    
    public function admin_export($survey_id){
        $this->autoRender = false;
        $this->loadModel("Survey");
        $this->loadModel("ResponsesQuestionsOption");
        $this->loadModel("Question");
        $this->loadModel("QuestionOption");
        $this->loadModel("SurveyQuestion");
        $this->loadModel("Answer");
        $questions = $this->SurveyQuestion->get_question_by_survey($survey_id);
        
        $responses = $this->Response->find('all',array('conditions'=>array('survey_id'=>$survey_id)));
        
        
        $questions_count = count($questions);
        $responses_count = count($responses);
        
        $survey = $this->Survey->read(null,$survey_id);
        
        $return = array();
        App::import('Vendor', 'Excel', array('file' => 'Excel' . DS . 'excel.php'));

        if($survey['Survey']['type']==Survey::EXAMINATION){
            for($i = 0; $i <= $questions_count; $i++){
                for($j = 0; $j <= $responses_count; $j++){
                    $return[$i][$j]="";
                    if($i == 0 && $j == 0){
                        $return[0][0] = $survey['Survey']['title'];
                    }
                    if($i >= 1){
                        $return[$i][0]=$questions[$i-1]['Question']['title'];
                    }
                    if($i >= 1 && $j >= 1){
                        $option = $this->ResponsesQuestionsOption->get_option($responses[$j-1]["Response"]['id'],$questions[$i-1]['Question']['id']);
                        $answer = $this->Answer->get_answers_by_questions($questions[$i-1]['Question']['id']);
                        $ret = array();
                        foreach ($option as $value) {
                            $check = 0;
                            if(in_array($value['Option']['id'], $answer)){
                                $check=1;
                            }
                            $ret[]=array('check'=>$check,'title'=>$value['Option']['title']);
                        }
                        $return[$i]['user'][$j] = "{$responses[$j-1]['Customer']['f_name']} {$responses[$j-1]['Customer']['l_name']}";
                        $return[$i]['ans'][$j]= $ret;
                    }
                }
            }
            //pr($return); die;
            if(!empty($return)){
                $xls = new ExcelPoll();      
                $xls->buildXlsExamination($return);
            } 
        }else{
            for($i = 0; $i <= $questions_count; $i++){
                for($j = 0; $j <= $responses_count; $j++){
                    $return[$i][$j]="";
                    if($i == 0 && $j == 0){
                        $return[0][0] = $survey['Survey']['title'];
                    }
                    if($i >= 1){
                        $return[$i][0] = $questions[$i-1]['Question']['title'];
                    }
                    if($i >= 1 && $j >= 1){
                        $opt=$this->ResponsesQuestionsOption->get_option($responses[$j-1]["Response"]['id'],$questions[$i-1]['Question']['id']);
                        $ret = array();
                        foreach ($opt as $value) {
                            if($value['ResponsesQuestionsOption']['option_id']==0){
                                $ret[]=$value['ResponsesQuestionsOption']['extend'];
                            }else{
                                $ret[]=$value['Option']['title'];
                            }
                        }
                        $return[$i]['user'][$j] = "{$responses[$j-1]['Customer']['f_name']} {$responses[$j-1]['Customer']['l_name']}";
                        $return[$i]['ans'][$j]=$ret;
                    }
                }
            }
            if(!empty($return)){
                $xls = new ExcelPoll();      
                $xls->buildXlsInvestigate($return);
            } 
        }
        pr($return);  die('>>>>>');
    }
}